package com.cpi.cornpp.transports.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.form.TransportForm;
import com.cpi.cornpp.transports.view.CostElementView;
import com.cpi.cornpp.transports.view.TransportView;

public class DeleteTransportCostElementAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String SHOW_NEW_TRANSPORT_FORWARD = "showNewTransport";
	private static final String SHOW_EDIT_TRANSPORT_FORWARD = "showEditTransport";
	private static final String FAILURE_FORWARD = "error";
	private static final String COMMON_ERROR_MESSAGE_KEY = "error.message.common.unexpectedError";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws TransportException{
		int index = 0;
		TransportForm transportForm = null;
		TransportView transportView = null;
		CostElementView costElementView = null;
		CostElementView deletedCostElementView = null;
		List costElementsView = null;
		List deletedTransportCostElements = null;
		List addedTransportCostElements = null;
		HttpSession session = null;
		ActionForward forward = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		
		try{
			
			session = request.getSession(false);
			transportView = (TransportView) session.getAttribute("transportView");
			deletedTransportCostElements = (List) session.getAttribute("deletedTransportCostElements");
			addedTransportCostElements = (List) session.getAttribute("addedTransportCostElements");
			
			if ((form != null) && (transportView != null)){
				transportForm = (TransportForm) form;
				costElementsView = transportView.getTransportCostElements();
				deletedTransportCostElements = (CollectionUtils.isNotEmpty(deletedTransportCostElements)) ? deletedTransportCostElements : new ArrayList(0);
				
				if ((CollectionUtils.isNotEmpty(costElementsView)) && ((transportForm.getCostElementIndex() >= 0) && (transportForm.getCostElementIndex() <= costElementsView.size()))){
					deletedCostElementView = (CostElementView) costElementsView.remove(transportForm.getCostElementIndex());
					deletedTransportCostElements.add(deletedCostElementView);
					
					if (CollectionUtils.isNotEmpty(addedTransportCostElements)){
						
						for (Iterator itr = addedTransportCostElements.iterator(); itr.hasNext(); ){
							costElementView = (CostElementView) itr.next();
							
							if (costElementView.getChargeTypeId() == deletedCostElementView.getChargeTypeId()){
								break;
							}
							
							index++;
						}
						
						addedTransportCostElements.remove(index);
					}
					
					transportView.setTransportShortName(transportForm.getCode());
					transportView.setTransportDescription(transportForm.getDescription());
					transportView.setTransportCapacity((StringUtils.isNotEmpty(transportForm.getCapacity())) ? Double.parseDouble(transportForm.getCapacity()) : 0.0D);
					transportView.setTransportPriority((StringUtils.isNotEmpty(transportForm.getPriority())) ? Integer.parseInt(transportForm.getPriority()) : 0);
					transportView.setTransportNomenclature(transportForm.getNomenclature());
				}
				
				session.setAttribute("transportView", transportView);
				session.setAttribute("deletedTransportCostElements", deletedTransportCostElements);
				request.setAttribute("transportView", transportView);
				
				forward = (StringUtils.isNotEmpty(transportForm.getId())) ? mapping.findForward(SHOW_EDIT_TRANSPORT_FORWARD) : mapping.findForward(SHOW_NEW_TRANSPORT_FORWARD);
			} else {
				TransportException tre = new TransportException(COMMON_ERROR_MESSAGE_KEY, TransportException.LAYER_ACTION, TransportException.ACTION_INSERT);
				
				throw tre;
			}
			
		} catch(Exception e){
			TransportException tre = new TransportException(e , TransportException.LAYER_ACTION, TransportException.ACTION_DELETE);;
			
			session.setAttribute(CUSTOM_EXCEPTION, tre);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw tre;
		}
		
		return forward;
	}
}